/*
 * @Description:
 * @Author: Junhuang
 * @Date: 2022-06-29 11:00:02
 * @LastEditTime: 2022-07-15 14:32:11
 * @LastEditors: Junhuang
 * @Reference:
 */
'use strict';

const Controller = require('egg').Controller;

class GiftController extends Controller {
  async index() {
    const { ctx, app } = this;
    const res = await app.model.Gift.findAll();
    return ctx.apiSuccesss(res);
  }

  async listPage() {
    const { ctx, app } = this;
    let res = await app.model.Gift.findAll();
    res = JSON.parse(JSON.stringify(res));
    let page = 1;
    const gifts = [];
    if (res && res.length > 0) {
      page = parseInt((res.length - 1) / 8 + 1);
      for (let i = 0; i < page; i++) {
        const arr = [];
        for (let j = 0; j < 8 && j + i * 8 < res.length; j++) {
          arr.push(res[j + i * 8]);
        }
        gifts.push(arr);
      }
      return ctx.apiSuccesss({
        count: res.length,
        gifts,
      });
    }
  }
}

module.exports = GiftController;
